import http from "@ohos.net.http";
import image from "@ohos.multimedia.image";
import fileIo from "@ohos.file.fs";
import fileUri from "@ohos.file.fileuri";
const scanImageName = 'scan-step.jpg';
export class ImageUtil {
    static async saveImageByUrl(x16, y16) {
        const z16 = await urlToPixelMap(y16);
        return ImageUtil.saveImageByPixelMap(x16, z16);
    }
    static async saveImageByPixelMap(u16, v16) {
        const w16 = await ImageUtil.createImageFile(u16, v16);
        return w16;
    }
    static async createImageFile(p16, q16) {
        const r16 = await ImageUtil.transferPixelMap2Buffer(q16);
        const s16 = fileIo.openSync(p16.cacheDir + '/' + scanImageName, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE);
        await fileIo.write(s16.fd, r16);
        await fileIo.close(s16.fd);
        const t16 = fileUri.getUriFromPath(s16.path);
        return t16;
    }
    static transferPixelMap2Buffer(i16) {
        return new Promise((j16, k16) => {
            let l16 = { format: 'image/jpeg', quality: 98 };
            const m16 = image.createImagePacker();
            m16.packing(i16, l16).then((o16) => {
                j16(o16);
            }).catch((n16) => {
                k16();
            });
        });
    }
}
export function getPixelMapInt32ArrayData(a16) {
    return new Promise((b16, c16) => {
        let d16 = a16.getPixelBytesNumber();
        console.log('getPixelMapInt32ArrayData pixelBytesNumber: ' + d16);
        let e16 = new ArrayBuffer(d16);
        let f16 = new Int32Array(e16);
        a16.readPixelsToBuffer(e16, (g16, h16) => {
            if (g16) {
                console.warn('getPixelMapInt32ArrayData error code is  ' + g16.code);
                console.warn('getPixelMapInt32ArrayData error msg is ' + g16.message);
                c16(g16);
            }
            else {
                console.log('getPixelMapInt32ArrayData success');
                console.log('getPixelMapInt32ArrayData readPixelsToUnit8Arr size:' + f16.length);
                console.log('getPixelMapInt32ArrayData readPixelsToUnit8Arr:' + JSON.stringify(f16));
                b16(f16);
            }
        });
    });
}
export function urlToPixelMap(u15) {
    return new Promise((v15, w15) => {
        http.createHttp().request(u15, { expectDataType: http.HttpDataType.ARRAY_BUFFER }).then(async (y15) => {
            let z15 = await image.createImageSource(y15.result)
                .createPixelMap();
            v15(z15);
        }).catch((x15) => {
            w15(x15);
        });
    });
}
export async function calculateLuminance(g15, h15 = false) {
    let i15 = g15.getPixelBytesNumber();
    let j15 = new ArrayBuffer(i15);
    await g15.readPixelsToBuffer(j15);
    const k15 = new Uint8Array(j15);
    let l15 = 0;
    const m15 = h15 ? 4 : 16;
    const n15 = h15 ? 0.25 : 0.0625;
    for (let o15 = 0; o15 < k15.length; o15 += m15) {
        const p15 = k15[o15 + 2];
        const q15 = k15[o15 + 1];
        const r15 = k15[o15];
        const s15 = k15[o15 + 3];
        const t15 = (0.3 * p15 + 0.59 * q15 + 0.11 * r15);
        if (t15 < 128) {
            l15++;
        }
        else {
        }
    }
    return (l15 / k15.length) / n15;
}
